package com.md.config.database.slave;

import com.md.util.EncryptionUtils;
import com.md.util.ObjectUtil;
import lombok.Data;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;

@Data
public class DataSourceProperty {
  private String url;
  private String username;
  private String password;
  private String driver;
  private Integer weight;
  private String unique;

  public Integer getWeight() {
    if (ObjectUtil.isEmpty(weight)) {
      return 1;
    }
    return weight;
  }

  public DataSource newInstanceDataSource() {
    return DataSourceBuilder.create()
      .url(url)
      .username(username)
      .password(password)
      .driverClassName(driver)
      .build();
  }

  public String getUnique() {
    if (ObjectUtil.isEmpty(unique)) {
      unique = EncryptionUtils.md5Encryption(url + username + password + driver);
    }
    return unique;
  }
}
